﻿using Microsoft.AspNetCore.Mvc.Filters;
using ZingBlog.Domain.Entities;

namespace ZingBlog.Web.Filters
{
    public class GlobalAuthorizeFilter : IAsyncAuthorizationFilter
    {
        public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
        {
            var authorization = context.HttpContext.Request.Headers["authorization"];
            var adminPaths = new List<string>() {
                "admin/","permissions/","blog/","sysset/"
            };
            var exceptionalPaths = new List<string>() { 
                "admin/login"
            };
            var contextPath = context.HttpContext.Request.Path.ToString().ToLower();
            if (adminPaths.Any(p => contextPath.Contains(p)) && !exceptionalPaths.Any(p => contextPath.Contains(p)))
            {
                if (OperatorProvider.Provider.GetCurrent() == null)
                {
                    context.HttpContext.Response.Redirect("/admin/login/index");
                }
            }
        }
    }
}
